package com.tk.exam.web.admin;

import com.tk.exam.exception.ApiException;
import com.tk.exam.http.Response;
import com.tk.exam.interceptor.NoCheckHttpHeader;
import com.tk.exam.service.StuScoreService;
import com.tk.exam.utils.PageInfo;
import com.tk.exam.vo.paper.SearchPaperListResp;
import com.tk.exam.vo.score.SearchStuScoreListReq;
import com.tk.exam.vo.score.SearchStuScoreListResp;
import com.tk.exam.vo.score.UpStuScoreReq;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("tk/exam/admin/stuScore")
@Slf4j
public class StudentScoreController {
    @Autowired
    private StuScoreService stuScoreService;

    /**
     * 查询某个考试的成绩
     * @param req
     * @return
     */
    @PostMapping(value = "/getListByPage")
    public Response searchStudentScoreListByPageWithPage(@RequestBody SearchStuScoreListReq req){
        if(Objects.isNull(req.getExamId()))
            throw ApiException.of("examId不能为空");
        //查询分页数据
        com.github.pagehelper.PageInfo<SearchStuScoreListResp> pageInfoHelper = stuScoreService.searchStudentScoreListByPage(req.getExamId());
        PageInfo pageInfo = new PageInfo(pageInfoHelper);
        return Response.succeed(pageInfo);
    }

    @PostMapping(value = "/upStuOffLineScore")
    public Response upStuOffLineScore(@RequestBody List<UpStuScoreReq> reqs){
        //todo:测试当list中某一个元素的参数无值时正常否
        reqs.parallelStream().forEach(
                req -> {
                    if(Objects.isNull(req.getScoreId())||0==req.getScoreId())
                        throw ApiException.of("成绩表id不能为空");
                    if(Objects.isNull(req.getOfflineSocre())||0.0==req.getOfflineSocre())
                        throw ApiException.of("考试成绩不能为空");
                }
        );

        stuScoreService.upStuOffLineScore(reqs);
        return Response.succeed();
    }

    @NoCheckHttpHeader
    @GetMapping(value = "/exportStudentScore")
    public String exportStudentScore(String examId){
        if(StringUtils.isEmpty(examId)){
            throw ApiException.of("examId不能为空");
        }
        //查询分页数据
        int i = stuScoreService.exportStudentScore(examId);
        return null;
    }

}
